using System;
using System.Linq;
using System.Collections.Generic;
using log4net;
using System.Reflection;
using System.IO;

namespace ODSDataExtraction
{
	public class ExtractorsPipeline
	{
		private static readonly ILog logger = LogManager.GetLogger(typeof(ExtractorsPipeline));
		private LinkedList<IDataExtractor> extractors = new LinkedList<IDataExtractor>();
				
		public ExtractorsPipeline ()
		{
			this.AddDefaultExtractors();
		}
		
		public void AddExtractorsFrom(string folderPath)
		{
			
		}
		
		public void AddExtractor(IDataExtractor extractor)
		{
			this.extractors.AddLast(extractor);
		}
		
		public void AddExtractorsFrom(Assembly assembly)
		{
			if(logger.IsDebugEnabled)
				logger.DebugFormat("Searching for extractors in {0}.", assembly);
			var types = assembly.GetTypes();
			foreach(var type in types)
			{
				if(type.IsClass && !type.IsAbstract)
				{
					if(type.BaseType.Equals(typeof(DataExtractorBase)))
					{
						if(logger.IsDebugEnabled)
							logger.DebugFormat("Found extractor {0}.", type);
						object extractor = Activator.CreateInstance(type);
						this.AddExtractor((IDataExtractor)extractor);
					}
				}
			}
		}
		
		public IDictionary<string, string> ExtractData(FileInfo file)
		{
			Dictionary<string, string> result = new Dictionary<string, string>();
			foreach(IDataExtractor extractor in this.extractors)
			{
				extractor.ExtractData(file, result);
			}
			return result;
		}
		
		public IEnumerable<IDictionary<string, string>> ExtractData(string folderPath)
		{
			throw new NotImplementedException();
		}
		
		private void AddDefaultExtractors()
		{
			this.AddExtractorsFrom(this.GetType().Assembly);
		}
	}
}

